package com.sc.controller;

import com.github.pagehelper.PageInfo;
import com.sc.pojo.Title;
import com.sc.pojo.Titletype;
import com.sc.service.ExamService;
import com.sc.service.TitleAdminService;
import com.sc.service.TitleTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/ttc")
public class TitleTypeController {
    @Autowired
    TitleTypeService tts;
    @Autowired
    TitleAdminService tas;
    @Autowired
    ExamService es;
    @RequestMapping("/show")
    public String show(@RequestParam(defaultValue = "1")Integer pageNum, Map m){
        PageInfo<Titletype> p = tts.show(pageNum, 3);
        m.put("p",p);
        return "/exammodol/titleType/list";
    }
    //增加科目
    @RequestMapping("/add")
    public String add(Titletype title){
        tts.add(title);
        return "redirect:/ttc/show";
    }
    //判断科目是否存在
    @RequestMapping(value = "/checkType" ,produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String checkType(String type){
        Titletype titletype = tts.checkType(type);
        if (titletype != null){
            return "科目已经存在";
        }
        return "科目不存在可以添加";
    }
//    删除多个科目
    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids){
        if (ids != null) {
            for (Integer id:ids) {
                String type = tts.showOne(id).getType();
                es.updateType(type);
            }
            tas.updateAll(ids);
            tts.deleteAll(ids);
        }
        return "redirect:/ttc/show";
    }
    //删除单个科目
    @RequestMapping("/delete")
    public String delete(Integer id){
        String type = tts.showOne(id).getType();
        es.updateType(type);
        tas.updateOne(id);
        tts.delete(id);
        return "redirect:/ttc/show";
    }
    //去编辑页面
    @RequestMapping("/toEdit")
    public String toEdit(Integer id,Map m){
        Title title = tas.editSelect(id);
        m.put("titleOne",title);
        return "/exammodol/after/edit";
    }


    @RequestMapping("/toAdd")
    public String toAdd(){
        return "/exammodol/titleType/add";
    }


}
